package com.gomore.opple.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gomore.opple.R;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ActionBarTitle extends RelativeLayout {
    @Bind(R.id.layout_left)
    View layout_left;
    @Bind(R.id.layout_right)
    View layout_right;
    @Bind(R.id.iv_left)
    ImageView iv_left;
    @Bind(R.id.iv_right)
    ImageView iv_right;
    @Bind(R.id.tv_left)
    TextView tv_left;
    @Bind(R.id.tv_right)
    TextView tv_right;
    @Bind(R.id.tv_title)
    TextView tv_title;


    private Context mContext;

    private String leftText, rightText, title;
    private Drawable leftIcon, rightIcon;
    private boolean leftIconVisible, leftTextVisible, rightIconVisible, rightTextVisible, titleVisible;

    private ActionBarTitleClickListener clickListener;

    public ActionBarTitle(Context context) {
        this(context, null);
    }

    public ActionBarTitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionBarTitle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ActionBarTitle);
        leftText = typedArray.getString(R.styleable.ActionBarTitle_text_left);
        rightText = typedArray.getString(R.styleable.ActionBarTitle_text_right);
        title = typedArray.getString(R.styleable.ActionBarTitle_text_title);
        leftIcon = typedArray.getDrawable(R.styleable.ActionBarTitle_icon_left);
        rightIcon = typedArray.getDrawable(R.styleable.ActionBarTitle_icon_right);
        leftIconVisible = typedArray.getBoolean(R.styleable.ActionBarTitle_icon_left_visible, true);
        leftTextVisible = typedArray.getBoolean(R.styleable.ActionBarTitle_text_left_visible, true);
        rightIconVisible = typedArray.getBoolean(R.styleable.ActionBarTitle_icon_right_visible, true);
        rightTextVisible = typedArray.getBoolean(R.styleable.ActionBarTitle_text_right_visible, true);
        titleVisible = typedArray.getBoolean(R.styleable.ActionBarTitle_title_visible, true);
        typedArray.recycle();
        initViews();
    }

    private void initViews() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_title_bar, null);
        addView(rootView);

        ButterKnife.bind(this, rootView);
        initActionBar();
    }

    private void initActionBar() {
        //左侧文字
        if (!TextUtils.isEmpty(leftText)) {
            tv_left.setText(leftText);
        }
        //左侧图标
        if (leftIcon != null) {
            iv_left.setImageDrawable(leftIcon);
        }
        //中间标题
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        //右侧文字
        if (!TextUtils.isEmpty(rightText)) {
            tv_right.setText(rightText);
        }
        //左侧文字可见性
        if (leftTextVisible) {
            tv_left.setVisibility(View.VISIBLE);
        } else {
            tv_left.setVisibility(View.GONE);
        }
        //左侧图标可见性
        if (leftIconVisible) {
            iv_left.setVisibility(View.VISIBLE);
        } else {
            iv_left.setVisibility(View.GONE);
        }
        //标题可见性
        if (titleVisible) {
            tv_title.setVisibility(View.VISIBLE);
        } else {
            tv_title.setVisibility(View.GONE);
        }
        //右侧文字可见性
        if (rightTextVisible) {
            tv_right.setVisibility(View.VISIBLE);
        } else {
            tv_right.setVisibility(View.GONE);
        }
        //右侧图标可见性
        if (rightIconVisible) {
            iv_right.setVisibility(View.VISIBLE);
        } else {
            iv_right.setVisibility(View.GONE);
        }
        tv_title.setTextColor(mContext.getResources().getColor(R.color.gray_text));
        //左侧图标响应事件
        layout_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null) {
                    clickListener.onLeftIconClick();
                }
            }
        });
        //右侧图标响应事件
        layout_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null) {
                    clickListener.onRightIconClick();
                }
            }
        });
    }


    public void setClickListener(ActionBarTitleClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }

    }

    public void setLeftIcon(int id) {
        iv_left.setBackgroundResource(id);
    }

    public interface ActionBarTitleClickListener {
        /**
         * 左侧图标点击事件
         */
        void onLeftIconClick();

        /**
         * 右侧图标点击事件
         */
        void onRightIconClick();
    }
}
